$(function () {
    //layui初始化
    layui.use(['form', 'upload'], function () {
        upload = layui.upload;

        //上传视频至七牛云
        var videoFileName = '';
        upload.render({
            elem: '.uploadVideo'
            , url: $("#uploadVideo").val()
            , auto: false
            , accept: 'video'
            , size: 0
            , data: {
                token: $('.uploadVideo').attr('data-token'),
                key: function () {
                    return videoFileName;
                }
            }
            , before: function (obj) {
                var i = this.item.index('.uploadVideo');
                obj.preview(function (index, file, result) {
                    $('.videoPath:eq(' + i + ')').attr('src', result);
                });
            }
            , choose: function (obj) {
                obj.preview(function (index, file) {
                    videoFileName = getId();
                    obj.upload(index, file)
                });
            }
            , done: function (data) {
                layer.msg('上传成功');
                $("#video_path").val(data.key);
            }
            , progress: function (n, elem, res, index) {
                var percent = n + '%' //获取进度百分比
                element.progress('video', percent); //可配合 layui 进度条元素使用
                element.progress('video-' + index, n + '%'); //进度条
            }
            , error:function(){
                layer.closeAll('loading');
            }
        });

        //上传音频至七牛云
        var audioFileName = '';
        upload.render({
            elem: '.uploadAudio'
            , url: $("#uploadVideo").val()
            , auto: false
            , accept: 'video'
            , size: 0
            , data: {
                token: $('.uploadAudio').attr('data-token'),
                key: function () {
                    return audioFileName;
                }
            }
            , before: function (obj) {
                var i = this.item.index('.uploadAudio');
                obj.preview(function (index, file, result) {
                    $('.audioLink:eq(' + i + ')').attr('src', result);
                });
            }
            , choose: function (obj) {
                obj.preview(function (index, file) {
                    audioFileName = getId();
                    obj.upload(index, file)
                });
            }
            , done: function (data) {
                layer.msg('上传成功');
                $("#audio_link").val(data.key);
            }
            , progress: function (n, elem, res, index) {
                var percent = n + '%' //获取进度百分比
                element.progress('audio', percent); //可配合 layui 进度条元素使用
                element.progress('audio-' + index, n + '%'); //进度条
            }
            , error:function(){
                layer.closeAll('loading');
            }
        });

        upload.render({
            elem: '.uploadVideoLocal'
            , url: $("#uploadVideo").val()
            , accept: 'video'
            , size: 0
            , before: function (obj) {
                var i = this.item.index('.uploadVideo');
                obj.preview(function (index, file, result) {
                    $('.videoPath:eq(' + i + ')').attr('src', result);
                });
            }
            , done: function (data) {
                layer.closeAll();
                if (data.code == 200) {
                    $("#video_path").val(data.data.url);
                    notify.success(data.msg);
                } else {
                    notify.error(data.msg);
                }
            }
            , progress: function (n, elem, res, index) {
                var percent = n + '%' //获取进度百分比
                element.progress('video', percent); //可配合 layui 进度条元素使用
                element.progress('video-' + index, n + '%'); //进度条
            }
            , error:function(){
                layer.closeAll('loading');
            }
        });
    })
});

function getId() {
    let date = Date.now();
    let rund = Math.ceil(Math.random()*10000)
    id = date + '' + rund;
    return id;
}